package com.ruoyi.cs.base.sort.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cs.base.sort.domain.Sort;
import com.ruoyi.cs.base.sort.service.ISortService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 客户/供应商分类
 * @author Drj 2021/6/28
 */
@RestController
@RequestMapping("/cs/sort")
@Api(value = "分类模块", tags = "客户/供应商分类接口")
public class SortController extends BaseController {
    @Autowired
    private ISortService sortService;

    /**
     * 获取客户/供应商分类列表
     * 需前端处理形成树形结构
     * @param sort
     * @return
     */
    @ApiOperationSupport(includeParameters = {"Sort.sortId", "Sort.sortName"})
    @ApiOperation(value = "获取客户/供应商分类列表",notes = "查询所有客户/供应商分类信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Sort sort) {
        startPage();
        List<Sort> baseUnitList = sortService.selectSortList(sort);
        return getDataTable(baseUnitList);
    }

    /**
     * 导出客户/供应商分类
     *
     * @param sort
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody Sort sort) {
        List<Sort> list = sortService.selectSortList(sort);
        ExcelUtil<Sort> util = new ExcelUtil<Sort>(Sort.class);
        return util.exportExcel(list, "客户/供应商分类数据");
    }

    /**
     * 新增客户/供应商分类
     *
     * @param sort
     * @return
     */
    @ApiOperation(value = "新增客户/供应商分类", notes = "新增客户/供应商分类")
    @ApiImplicitParam(name = "sort", value = "新增客户/供应商分类", dataType = "Sort")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Sort sort) {

        return toAjax(sortService.insertSort(sort));
    }

    /**
     * 修改客户/供应商分类
     *
     * @param sort
     * @return
     */
    @ApiOperation(value = "修改客户/供应商分类", notes = "修改客户/供应商分类")
    @ApiImplicitParam(name = "sort", value = "修改客户/供应商分类", dataType = "Sort")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody Sort sort) {
        return toAjax(sortService.updateSort(sort));
    }

    /**
     * 删除客户/供应商分类
     * 仅修改删除标志位
     *
     * @param sort
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Sort sort) {
        return toAjax(sortService.updateSortDeleteFlag(sort));
    }


}
